package top.malaoshi.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import top.malaoshi.entity.Notice;
import top.malaoshi.model.JsonResult;

import javax.annotation.Resource;

@RestController
@RequestMapping("/follow2")
public class FollowController2 {

    //notice的url
//    public static final String NOTICE_URL="http://localhost:8080";
    public static final String NOTICE_URL="http://notice-service";

    @Resource
    private RestTemplate restTemplate;


    @PostMapping(value="/follow")
    public JsonResult follow(Notice notice){
        //操作数据

        //推送消息，是调用另一个微服务
        return restTemplate.postForObject(NOTICE_URL+"/sendNotice",notice, JsonResult.class);
    }

    @GetMapping(value="/getNotice/{id}")
    public JsonResult getNotice(@PathVariable("id") String id){
        return restTemplate.getForObject(NOTICE_URL+"/getNoticeById/"+id, JsonResult.class);
    }

    @GetMapping(value="/getNotice2/{id}")
    public JsonResult getNotice2(@PathVariable("id") String id) {
        ResponseEntity<JsonResult> re = restTemplate.getForEntity(NOTICE_URL + "/getNoticeById/{1}", JsonResult.class, id);
        if (re.getStatusCode().is2xxSuccessful()) {
            return re.getBody();
        } else {
            return new JsonResult(-1, "失败！");
        }
    }
}
